# ifndef __ListView_H_
# define __ListView_H_

void AddColumn               (HWND _hList, int _i, const char * _strText, bool _bAlignRight = false);
void AutoSizeColumns         (HWND _hList);

void SetImageListForColumns  (HWND _hList, HIMAGELIST _hil);
void SetImageForColumn       (HWND _hList, size_t _cCol, size_t _cImage);
void RemoveImageForColumn    (HWND _hList, size_t _cCol);

void __cdecl InsertItem      (HWND _hList, size_t _cItem, LPARAM _lParam, const char * _strFormat, ...);
void __cdecl SetItemText     (HWND _hList, size_t _cItem, const char * _strFormat, ...);
void         SetItemIcon     (HWND _hList, size_t _cItem, size_t _cIconIndex);
void __cdecl SetSubItemText  (HWND _hList, size_t _cItem, size_t _cSubItem, const char * _strFormat, ...);
LPARAM       GetLParam       (HWND _hList, size_t _cItem);
int          GetImage        (HWND _hList, size_t _cItem);

void SetSelection            (HWND _hList, size_t _cItem);
void UpdateSelectionMark     (HWND _hList);
void RemoveSelection         (HWND _hList);
void EnsureSelectionVisible  (HWND _hList);
void SelectColumn            (HWND _hList, size_t _cColumn);

class CListView {
  public:
	CListView ();
	~CListView () {Destroy ();}

	operator HWND () const {return m_hList;}

	void Init (HWND _hWndParent, WORD _wID, const char * _strSetting = NULL, int _nDefaultSortColumn = -1);
	void Destroy ();

	void SetExtStyle (DWORD _dwExtStyle);
	void SetImageList (HIMAGELIST _hil, bool _bShared);

	void LoadHeaders ();
	void SaveHeaders ();

	void SetSorting (PFNLVCOMPARE _pfnCompare, LPARAM _lParamSort) {
		m_pfnCompare = _pfnCompare;
		m_lParamSort = _lParamSort;
	}

	void Sort ();
	void SetSortingColumn (int _nCol) {m_nSortingColumn = _nCol;}
	int GetSortingColumn () const {return m_nSortingColumn;}
	bool IsSortingAscending () const {return m_bSortingAscending;}

	void Sort_OnColumnClick       (const NMHDR * _pInfo);
	void PopupMenu_OnHeaderRClick (const NMHDR * _pInfo);

	void SelectByLParam (LPARAM _lParam);

  private:
	string_t m_strSetting;

	HWND m_hList;
	HIMAGELIST m_hilSortingArrows;

	PFNLVCOMPARE m_pfnCompare;
	LPARAM m_lParamSort;
	int  m_nSortingColumn;
	bool m_bSortingAscending;

	static int CALLBACK _CompareItems (LPARAM _lParam1, LPARAM _lParam2, LPARAM _lData);
};

# endif // __ListView_H_